package com.bob.common.resources.exec;

import com.bob.common.DataStore;
import com.bob.common.bean.Constant;
import com.bob.common.bean.DiffItem;
import com.bob.common.bean.PomBaseEntity;
import com.bob.common.bean.PomEntity;
import com.bob.common.resources.ResourcesExec;
import com.bob.common.utils.FileUtil;
import com.bob.common.utils.PomUtil;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 处理内部项目依赖
 * @author bob
 */
public class InsideDependencyLibExec implements ResourcesExec {
    /**
     * @param baseDir         项目路径
     * @param outputDirectory
     * @param patchOutPath    patchOutPath/2021-08-07-12-12-54
     * @param finalName
     */
    @Override
    public void exec(File baseDir, File outputDirectory, File patchOutPath, String finalName) {
        final String pathPrefix = patchOutPath.getAbsolutePath() +File.separator+ finalName;
        File patchDir = new File(pathPrefix+ File.separator + Constant.WEB_INF + File.separator + Constant.lib+ File.separator);
        if(!patchDir.exists()){
            FileUtil.mkdir(patchDir);
        }

        final String s = DataStore.getDataStore().getPomAbsolutePathRef().get(baseDir.getAbsolutePath());
        PomEntity pomEntity = DataStore.getDataStore().getPomEntityMap().get(s);
        // 非本项目的变更文件
        List<DiffItem> diffItems = DataStore.getDataStore().getDiffItems().stream().filter(diffItem -> !diffItem.getGroupName().equals(pomEntity.getArtifactId())).collect(Collectors.toList());
        // 寻找到所有的依赖
        List<String> artifactIds = diffItems.stream().map(DiffItem::getGroupName).distinct().collect(Collectors.toList());
        // /home/bob/文档/workspace/cczyz/cczyz_weixin/target/cczyz_weixin-0.0.1-SNAPSHOT/WEB-INF/lib/
        File file = new File(outputDirectory + File.separator + finalName + File.separator + Constant.WEB_INF + File.separator + Constant.lib+ File.separator);
        if(file.exists()){
            for (String artifactId:artifactIds) {
                // 确认变更文件是否属于本项目的依赖 从依赖传递中寻找
                PomBaseEntity dependence = PomUtil.existDependency(pomEntity,artifactId);
                if (dependence!=null){
                    PomEntity d = DataStore.getDataStore().getPomEntityMap().get(dependence.getArtifactId());
                    // /home/bob/文档/workspace/cczyz/cczyz_weixin/target/cczyz_weixin-0.0.1-SNAPSHOT/WEB-INF/lib/cczyz_base-0.0.1-SNAPSHOT.jar
                    String jar = file.getAbsolutePath()+ File.separator  + d.getFinalName() + "." + d.getPackaging();
                    File dependency = new File(jar);
                    if(dependency.exists()){
                        FileUtil.copy(dependency,patchDir,true);
                        logger.info(String.format("=== 复制依赖文件到补丁目录成功 ： "));
                        logger.info(String.format("=== \t\t\t\t  %s \t===>>\t %s ", dependency,patchDir));
                    }else{
                        logger.error(String.format("=== 依赖文件  %s  在目录 %s 找不到！", dependency.getAbsolutePath(),file.getAbsolutePath()));
                    }
                }
            }
        }else {
            logger.error(String.format("=== 文件夹  %s  找不到！", file.getAbsolutePath()));
        }


    }
}
